<?php
class smarty_ext_clients {

	function smarty_ext_clients() {
		global $setupdata, $mysqlclass, $clientsclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->clientsclass=$clientsclass;
	}
	
	function get_clients_form(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type` = 'block_client_form'", __LINE__, __FILE__);		
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row;
		return $result;
	}
	
	function get_page_permission($params){
		if (isset($params['parent']) && (strlen($params['parent']) > 0)){
			return $this->clientsclass->get_page_permission($params['parent']);
		} 
		return 0;
	}
	
	function get_client_permision($params){
		if (isset($params['group']) && isset($params['client']) && (strlen($params['group']) > 0) && (strlen($params['client']) > 0)){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id` = ".$params['group']." AND `client_id` =".$params['client'], __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) return 1;
		} 
		return 0;
	}
	
	function get_clients_group($params){
		if (isset($params['parent']) && (strlen($params['parent']) > 0)){
			return $this->clientsclass->group_get($params['parent']);
		} else {
			return $this->clientsclass->group_get();
		}
	}
	
	function get_group_info($params){
		if (isset($params['id']) && (strlen($params['id']) > 0)){
			return $this->clientsclass->group_get_info($params['id']);
		}
	}
	
	function get_form_elements($params){
		if (isset($params['parent']) && (strlen($params['parent']) > 0)){
			return $this->clientsclass->get_form_elem_data($params['parent']);
		}
	}
	
	function is_have_subgroup($params){
		if (isset($params['id']) && (strlen($params['id']) > 0)){
			$query = "SELECT `parent` FROM `".$this->setupdata['table_prefix']."client_groups` WHERE `id`=".$params['id'];
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
			if ($num == 0) return 0;
			else {
				$rows = mysql_fetch_array($sql, MYSQL_ASSOC);
				if ($rows['parent'] == 0) return 1;
				else return 0;
			}
		}
		return 0;
	}
	
	/*
	function get_registration_form($params){
		$result = array();
		if ((isset($params['id'])) && (strlen($params['id']) == 0)) return $result;
		$result = $this->clientsclass->get_form_elem_data_guest($params['id']);
		return $result;
	}
	*/
	
	function get_clients_info_by_group_id($params){
		//print_r($params);
		$result = array();
		if ((isset($params['id'])) && (strlen($params['id']) == 0)) return $result;
		if (!isset($params['order']) || (strlen($params['order']) == 0)) $params['order'] = "0";
		
		if (isset($params['page']) || isset($params['items_per_page'])){
			$params['page'] = intval($params['page']);
			if ($params['page'] == 0){
				$from = 0;
				$many = $params['items_per_page'];
			} elseif ($params['page'] == -1) {
				$from = 0;
				$many = '';
			} else {
				$from = $params['page']*$params['items_per_page']-$params['items_per_page'];
				$many = $params['items_per_page'];
			}
		} else {
			$from = 0;
			$many = 50;
		}

		$clients = array();
		if($many != '') {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id`=".$params['id']." ORDER BY `client_id` DESC LIMIT ".$from.", ".$many, __LINE__, __FILE__);
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id`=".$params['id']." ORDER BY `client_id` DESC ", __LINE__, __FILE__);
		}
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$clients[$row['client_id']] = $this->clientsclass->get_clients_info_by_id($row['client_id']);
			$clients[$row['client_id']]['groups'] = $this->clientsclass->get_one_client_groups($row['client_id']);
			if ((count($clients[$row['client_id']]) == 0) || (!isset($clients[$row['client_id']]['main']))) unset($clients[$row['client_id']]);
		}
		$result = $clients;
		
		if ($params['order'] != "0") {
			$funct='function cmp($a, $b){
				$z=\''.$params['order'].'\';
				if (!isset($a[\'data\'][$z][\'value\'])) $a[\'data\'][$z][\'value\'] = "";
				if (!isset($b[\'data\'][$z][\'value\'])) $b[\'data\'][$z][\'value\'] = "";
				if (strtolower($a[\'data\'][$z][\'value\']) == strtolower($b[\'data\'][$z][\'value\'])) return 0;
				return (strtolower($a[\'data\'][$z][\'value\']) < strtolower($b[\'data\'][$z][\'value\'])) ? -1 : 1;
			}';
			eval ($funct);
			usort($result, "cmp");
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client_depend` WHERE `group_id`=".$params['id']." ORDER BY `client_id` DESC", __LINE__, __FILE__);
		$num = mysql_num_rows($sql);
		$page = 1;
		for ($i=1; $i <= $num; $i++) {
			$res['pages'][$page]['number']=$page;
			$res['pages'][$page]['from']=$i;
			if ($i+4 <= $num) $res['pages'][$page]['to']=$i+49;
			else $res['pages'][$page]['to']=$num;
			$i = $i + 49;
			$page = $page + 1;
		}
		
		$res['totalelements'] = $num;
		$res['itemsperpage'] = $many;
		$res['totalpages'] = count($res['pages']);
		$res['current_page'] = $res['pages'][$params['page']];
		if ($params['page'] > 1) $res['previous_page'] = $res['curent'] - 1;
		if ($res['totalelements'] > $params['page']) $res['next_page'] = $res['pages'][$params['page']+1];
		
		$r['list'] = $result;
		$r['pages'] = $res;
		
		return $r;
	}
	
	function get_all_clients_info_with_groups_and_order($params) {
		if (!isset($params['order']) || (strlen($params['order']) == 0)) $params['order'] = "";
		if (intval($params['page']) == 0) $params['page'] = 1;
		if (isset($params['page']) || isset($params['items_per_page'])){
			if ($params['page'] == 0){
				$from = 0;
				$many = $params['items_per_page'];
			} elseif ($params['page'] == -1) {
				$from = 0;
				$many = '';
			} else {
				$from = $params['page']*$params['items_per_page']-$params['items_per_page'];
				$many = $params['items_per_page'];
			}
		} else {
			$from = 0;
			$many = 50;
		}
		
		$result = array();
		if($many != '') {
			$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.`id`, `t1`.`email`, `t1`.`status`, `t1`.`regdate`, `t1`.`lastlogin` FROM `".$this->setupdata['table_prefix']."client` AS `t1`, `".$this->setupdata['table_prefix']."client_data` AS `t2` WHERE `t2`.`name` = '".$params['order']."' AND `t1`.`id` = `t2`.`parent` ORDER BY `t2`.`value` ASC LIMIT ".$from.", ".$many, __LINE__, __FILE__);
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.`id`, `t1`.`email`, `t1`.`status`, `t1`.`regdate`, `t1`.`lastlogin` FROM `".$this->setupdata['table_prefix']."client` AS `t1`, `".$this->setupdata['table_prefix']."client_data` AS `t2` WHERE `t2`.`name` = '".$params['order']."' AND `t1`.`id` = `t2`.`parent` ORDER BY `t2`.`value` ASC", __LINE__, __FILE__);
		}
		if (mysql_num_rows($sql) > 0){
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$result[$row['id']]['main'] = $row;
				$result[$row['id']] = $this->clientsclass->get_clients_info_by_id($row['id']);
				$result[$row['id']]['groups'] = $this->clientsclass->get_one_client_groups($row['id']);
			}
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.`id`, `t1`.`email`, `t1`.`status`, `t1`.`regdate`, `t1`.`lastlogin` FROM `".$this->setupdata['table_prefix']."client` AS `t1`, `".$this->setupdata['table_prefix']."client_data` AS `t2` WHERE `t2`.`name` = '".$params['order']."' AND `t1`.`id` = `t2`.`parent` ORDER BY `t2`.`value` ASC", __LINE__, __FILE__);
		$num = mysql_num_rows($sql);
		$page = 1;
		for ($i=1; $i <= $num; $i++) {
			$res['pages'][$page]['number']=$page;
			$res['pages'][$page]['from']=$i;
			if ($i+4 <= $num) $res['pages'][$page]['to']=$i+49;
			else $res['pages'][$page]['to']=$num;
			$i = $i + 49;
			$page = $page + 1;
		}
		
		$res['totalelements'] = $num;
		$res['itemsperpage'] = $many;
		$res['totalpages'] = count($res['pages']);
		$res['current_page'] = $res['pages'][$params['page']];
		if ($params['page'] > 1) $res['previous_page'] = $res['curent'] - 1;
		if ($res['totalelements'] > $params['page']) $res['next_page'] = $res['pages'][$params['page']+1];
		
		$r['list'] = $result;
		$r['pages'] = $res;
		
		return $r;
	}
	
	function get_all_clients_info_with_groups($params){
		if (!isset($params['order']) || (strlen($params['order']) == 0)) $params['order'] = "0";
		if (isset($params['page']) || isset($params['items_per_page'])){
			if ($params['page'] == 0){
				$from = 0;
				$many = $params['items_per_page'];
			} elseif ($params['page'] == -1) {
				$from = 0;
				$many = '';
			} else {
				$from = $params['page']*$params['items_per_page']-$params['items_per_page'];
				$many = $params['items_per_page'];
			}
		} else {
			$from = 0;
			$many = 50;
		}
		
		$result = array();
		if($many != '') {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client` ORDER BY `regdate` DESC LIMIT ".$from.", ".$many, __LINE__, __FILE__);
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client` ORDER BY `regdate` DESC", __LINE__, __FILE__);
		}
		if (mysql_num_rows($sql) > 0){
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$result[$row['id']]['main'] = $row;
				$result[$row['id']] = $this->clientsclass->get_clients_info_by_id($row['id']);
				$result[$row['id']]['groups'] = $this->clientsclass->get_one_client_groups($row['id']);
			}
		}
		
		if ($params['order'] != "0") {
			$funct='function cmp($a, $b){
				$z=\''.$params['order'].'\';
				if (!isset($a[\'data\'][$z][\'value\'])) $a[\'data\'][$z][\'value\'] = "";
				if (!isset($b[\'data\'][$z][\'value\'])) $b[\'data\'][$z][\'value\'] = "";
				if (strtolower($a[\'data\'][$z][\'value\']) == strtolower($b[\'data\'][$z][\'value\'])) return 0;
				return (strtolower($a[\'data\'][$z][\'value\']) < strtolower($b[\'data\'][$z][\'value\'])) ? -1 : 1;
			}';
			eval ($funct);
			usort($result, "cmp");
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."client` ORDER BY `regdate` DESC", __LINE__, __FILE__);
		$num = mysql_num_rows($sql);
		$page = 1;
		for ($i=1; $i <= $num; $i++) {
			$res['pages'][$page]['number']=$page;
			$res['pages'][$page]['from']=$i;
			if ($i+4 <= $num) $res['pages'][$page]['to']=$i+49;
			else $res['pages'][$page]['to']=$num;
			$i = $i + 49;
			$page = $page + 1;
		}
		
		$res['totalelements'] = $num;
		$res['itemsperpage'] = $many;
		$res['totalpages'] = count($res['pages']);
		$res['current_page'] = $res['pages'][$params['page']];
		if ($params['page'] > 1) $res['previous_page'] = $res['curent'] - 1;
		if ($res['totalelements'] > $params['page']) $res['next_page'] = $res['pages'][$params['page']+1];
		
		$r['list'] = $result;
		$r['pages'] = $res;
		
		return $r;
	}
	
	function get_client_groups($params){
		if (isset($params['id']) && (strlen($params['id']) > 0)){
			return $this->clientsclass->get_one_client_groups($params['id']);
		}
	}
	
	function get_client_info_by_id_admin($params){
		$client_info = $this->clientsclass->get_clients_info_by_id($params['id']);
		if (isset($this->setupdata['modules']['shop']['modules']['off']) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
			global $shopclass;
			$price_mod = array();
			if (isset($client_info['groups'])){
				foreach($client_info['groups'] as $value){
//					$price_mod1 = $shopclass->get_price_mod_by_product_id($value, 'client_group');
					$price_mod1 = $shopclass->get_off($value, 'client_group');
					if ($price_mod1 != 0){
						if (count($price_mod) > 0){
							if ($price_mod['num'] < $value['num']) $price_mod = $value;
						} else $price_mod = $price_mod1;
					}
				}
				if (count($price_mod) > 0) $client_info['price_mod'] = $price_mod;
			}
		}
		return $client_info;
	}

	function get_clients_info_by_id($params){
		$client_info = $this->clientsclass->get_clients_info_by_id($params['id']);
		if (isset($this->setupdata['modules']['shop']['modules']['off']) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
			global $shopclass;
			$price_mod = array();
			if (isset($client_info['groups'])){
				foreach($client_info['groups'] as $value){
//					$price_mod1 = $shopclass->get_price_mod_by_product_id($value, 'client_group');
					$price_mod1 = $shopclass->get_off($value, 'client_group');
					if ($price_mod1 != 0){
						if (count($price_mod) > 0){
							if ($price_mod['num'] < $value['num']) $price_mod = $value;
						} else $price_mod = $price_mod1;
					}
				}
				if (count($price_mod) > 0) $client_info['price_mod'] = $price_mod;
			}
		}
		return $client_info;
	}
	
	function get_all_fields(){
		$arr = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT DISTINCT `name` FROM `".$this->setupdata['table_prefix']."client_data` ORDER BY `name`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$arr[$row['name']] = stripslashes($row['name']);
		}	
		return $arr;
	}
	
	function get_pass_letter_data($params){
		$arr = array();
					
		if ($params['lang']) $clang = $params['lang'];
		else $clang = $this->setupdata['default_languages'];
		
		if ($clang == $this->setupdata['default_languages']){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group`='pass_letter' AND ('".$clang."'=`type` OR 'text'=`type`)", __LINE__, __FILE__);
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group`='pass_letter' AND '".$clang."'=`type`", __LINE__, __FILE__);
		}
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$arr[$row['name']] = stripslashes($row['value']);
		}	
		return $arr;
	}
}
?>